/*
 * $Id: UserStateObjectFactory.java 43 2007-05-14 10:18:31Z aaron.day $
 */
package org.crazydays.tapestry;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.engine.state.StateObjectFactory;

import org.hibernate.HibernateException;

import org.crazydays.core.data.PolisData;
import org.crazydays.core.data.PolisUser;
import org.crazydays.hibernate.HibernateUtil;

/**
 * UserStateObjectFactory
 */
public class UserStateObjectFactory
	implements StateObjectFactory
{
	/** log */
	private Log LOG = LogFactory.getLog(getClass());

	/** guest user name */
	private String guestUserName;

	/**
	 * Create guest user.
	 * 
	 * @return Polis guest user
	 */
	public Object createStateObject()
	{
		try {
			String guestUserName = getGuestUserName();
			if (guestUserName == null) {
				LOG.error("guestUserName == null");
				throw new ApplicationRuntimeException(
						"guestUserName == null");
			}

			PolisData user = HibernateUtil.loadPolisDataByName(
					PolisUser.class, guestUserName);

			LOG.debug("Loaded user " + user);

			return user;
		} catch (HibernateException e) {
			LOG.error("Unable to load user " + getGuestUserName(),
					e);
			return null;
		}
	}

	/**
	 * Set guest user name.
	 * 
	 * @param guestUserName Guest user name
	 */
	public void setGuestUserName(String guestUserName)
	{
		this.guestUserName = guestUserName;
	}

	/**
	 * Get guest user name.
	 * 
	 * @return Guest user name
	 */
	public String getGuestUserName()
	{
		return guestUserName;
	}
}
